package projectEuler.net;
/**
 * By considering the terms in the Fibonacci sequence whose values do not exceed
 * four million, find the sum of the even-valued terms.
 * 
 * @author xrose
 *
 */
public class Problem_002 {
	private static int f0, f1, fn;
	private static int sumEvenTerm;
	
	public static int sumEvenTermOfFibonaci(int n){
		init();
		calculateSumEvenTerm(n);
		putResult();
		return sumEvenTerm;
	}
	private static void init(){
		f0 = 1;
		f1 = 1;
		fn = f0+f1;
		sumEvenTerm = 0;
	}
	private static void calculateSumEvenTerm(int n){
		while(fn <= n){
			if(isEven(fn))
				sumEvenTerm+=fn;
			f0=f1;
			f1=fn;
			fn=f0+f1;
		}
	}
	private static boolean isEven(int iE){
		if(iE<=0) return false;
		if(iE%2 == 0) return true;
		return false;
	}
	private static void putResult(){
		System.out.printf("Result is: %d\n", sumEvenTerm);
	}
}
